package com.asset.inventory.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.asset.inventory.R
import com.asset.inventory.base.BaseApp
import com.asset.inventory.model.TaskBean

/**
 * 页面名称：盘点任务列表适配器
 * 创建时间  2021/03/23 20:13
 */
open class InventoryAdapter(
    private val inventoryList: ArrayList<TaskBean.Task>, private val itemClickListener: OnItemClickListener
): RecyclerView.Adapter<InventoryAdapter.ViewHolder>(){


    interface OnItemClickListener{
        fun enterInventoryDetailPage(position: Int)
        fun commit(position: Int)
        fun clear(position: Int)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val startInventoryView: RelativeLayout = itemView.findViewById(R.id.start_inventory)
        val commitView: RelativeLayout = itemView.findViewById(R.id.commit)
        val clearView: RelativeLayout = itemView.findViewById(R.id.clear)
        val taskName: TextView = itemView.findViewById(R.id.name)
        val createPerson: TextView = itemView.findViewById(R.id.createPerson)
        val startTime: TextView = itemView.findViewById(R.id.startTime)
        val total: TextView = itemView.findViewById(R.id.total)
        val endTime: TextView = itemView.findViewById(R.id.endTime)
        val waitCount: TextView = itemView.findViewById(R.id.waitCount)
        val finishCount: TextView = itemView.findViewById(R.id.finishCount)
        val commitLine: View = itemView.findViewById(R.id.commitLine)
    }

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): ViewHolder {
        val view = LayoutInflater.from(p0.context).inflate(R.layout.item_inventory,p0,false)
        return ViewHolder(view)
    }

    override fun getItemCount(): Int = inventoryList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val task = inventoryList[position]
        task.let {
            holder.taskName.text = it.inventoryPlanName
            holder.createPerson.text = "创建人：${it.inventoryPlanCreateUser}"
            holder.startTime.text = "开始时间：${it.inventoryPlanStartDate}"
            holder.total.text = "盘点总数：${it.dutyResultCount}"
            holder.endTime.text = "结束日期：${it.inventoryPlanEndDate}"
            holder.waitCount.text = "${it.dutyResultWaitCount}"
            holder.finishCount.text = "${it.dutyResultDoneCount}"
            if(!BaseApp.instance.isOnlineModel){
                holder.commitView.visibility = View.GONE
                holder.commitLine.visibility = View.INVISIBLE
            }else{
                holder.commitView.visibility = View.VISIBLE
                holder.commitLine.visibility = View.VISIBLE
            }
            holder.startInventoryView.setOnClickListener {
                itemClickListener.enterInventoryDetailPage(position)
            }

            holder.commitView.setOnClickListener {
                itemClickListener.commit(position)
            }

            holder.clearView.setOnClickListener {
                itemClickListener.clear(position)
            }
        }

    }
}